﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Security.Cryptography.X509Certificates;
namespace Common.Web
{
    public class OperCert
    {

        internal static byte[] ReadFile(string fileName)
        {

            FileStream f = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            int size = (int)f.Length;
            byte[] data = new byte[size];
            size = f.Read(data, 0, size);
            f.Close();
            return data;
        }

        public void Check()
        {
            try
            {
                X509Certificate2 x509 = new X509Certificate2();
                byte[] rawData = ReadFile(@"e:\test.cer");
                x509.Import(rawData);
                Console.WriteLine("{0}Subject: {1}{0}", Environment.NewLine, x509.Subject);
                Console.WriteLine("{0}Issuer: {1}{0}", Environment.NewLine, x509.Issuer);
                Console.WriteLine("{0}Version: {1}{0}", Environment.NewLine, x509.Version);
                Console.WriteLine("{0}Valid Date: {1}{0}", Environment.NewLine, x509.NotBefore);
                Console.WriteLine("{0}Expiry Date: {1}{0}", Environment.NewLine, x509.NotAfter);
                Console.WriteLine("{0}Thumbprint: {1}{0}", Environment.NewLine, x509.Thumbprint);
                Console.WriteLine("{0}Serial Number: {1}{0}", Environment.NewLine, x509.SerialNumber);
                Console.WriteLine("{0}Friendly Name: {1}{0}", Environment.NewLine, x509.PublicKey.Oid.FriendlyName);
                Console.WriteLine("{0}Public Key Format: {1}{0}", Environment.NewLine, x509.PublicKey.EncodedKeyValue.Format(true));
                Console.WriteLine("{0}Raw Data Length: {1}{0}", Environment.NewLine, x509.RawData.Length);
                Console.WriteLine("{0}Certificate to string: {1}{0}", Environment.NewLine, x509.ToString(true));
                Console.WriteLine("{0}Certificate to XML String: {1}{0}", Environment.NewLine, x509.PublicKey.Key.ToXmlString(false));
                X509Store store = new X509Store();
                store.Open(OpenFlags.MaxAllowed);
                store.Add(x509);
                store.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.Message);
            }
        }

        public X509Certificate2 FindCertificate(StoreLocation location, StoreName name, X509FindType findType, string findValue)
        {
            X509Store store = new X509Store(name, location);
            try
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection col = store.Certificates.Find(findType, findValue, true);
                return col[0];
            }
            finally { store.Close(); }
        }



        public List<string> GetCertList()
        {
            X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            List<string> keys = new List<string>();
            store.Open(OpenFlags.ReadOnly);
            bool isExist = false;
            //轮询存储区中的所有证书 
            foreach (X509Certificate2 myX509Certificate2 in store.Certificates)
            {
                keys.Add(myX509Certificate2.Subject);
            }
            store.Close();
            return keys;
        }
    }
}
